package com.zb.network.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.network.service.ConNetworkAuthenFieldService;
import com.zb.network.service.ConNetworkService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjNetworkAloneTeamChangeService;
import com.zb.service.ZjNetworkEmpService;
import com.zb.service.ZjNetworkService;
import com.zb.utils.TokenUtil;
import com.zb.vo.NetworkAloneTeamChangeCheckVo;
import com.zb.vo.NetworkSelectVo;
import com.zb.vo.NetworkVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 网点表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Api(tags = "服务商类接口")
@RestController
@RequestMapping("/network")
public class ZjNetworkController {
    @Resource
    private ZjNetworkService networkService;
    @Resource
    private ConNetworkService conNetworkService;
    @Resource
    private ZjNetworkEmpService networkEmpService;
    @Resource
    private ZjNetworkAloneTeamChangeService networkAloneTeamChangeService;

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "服务区域-服务商列表 - 新增或修改服务商")
    @ApiOperation("新增或修改服务商")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid NetworkVo networkVo) {
        AccounLoginInfo logintInfo = TokenUtil.getAccountLoginInfo(request);
        networkService.save(logintInfo, networkVo);
        return Result.success(null == networkVo.getId()? "新增服务商成功" : "修改服务商成功");
    }

    @ApiOperation("获取服务商列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody @Valid NetworkSelectVo networkSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取服务商列表成功").data(conNetworkService.getList(custAccId, networkSelectVo));
    }

    @ApiOperation("根据服务商ID获取服务商详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "服务商ID", required = true) Long id) {
        return Result.success("获取服务商详情成功").data("row", conNetworkService.getDetail(id));
    }

    @ApiOperation("获取服务商详情之服务商人员列表")
    @PostMapping("/getNetworkDetailEmpList/{networkId}")
    public Result getNetworkDetailEmpList(HttpServletRequest request, @PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId, @RequestBody PageUtil pageUtil) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取服务商详情人员列表成功").data(networkEmpService.getNetworkDetailEmpList(custAccId, networkId, pageUtil));
    }

    @ApiOperation("获取服务商详情之服务商人员列表的详情")
    @GetMapping("/getNetworkDetailEmpListDetail/{id}")
    public Result getDetail(HttpServletRequest request, @PathVariable @ApiParam(value = "服务人员ID", required = true) Long id,
                            @RequestParam(defaultValue = "0") @ApiParam(value = "服务商人员关联ID", required = true) Long empRelationId) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取服务人员详情成功").data("row", networkEmpService.getDetail(custAccId, id, empRelationId));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.DELETE, remark = "服务区域-服务商列表 - 根据服务人员关联ID删除服务商人员")
    @ApiOperation("根据服务人员关联ID删除服务商人员")
    @DeleteMapping("/deleteNetworkEmp/{empRelationId}")
    public Result deleteNetworkEmp(@PathVariable @ApiParam(value = "服务商人员关联ID", required = true) Long empRelationId) {
        networkEmpService.deleteNetworkEmp(empRelationId);
        return Result.success("删除服务商人员成功");
    }


    @ApiOperation("获取服务商详情之工单明细列表")
    @PostMapping("/getNetworkDetailOrderList/{networkId}")
    public Result getNetworkDetailOrderList(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取服务商详情工单明细列表成功").data(conNetworkService.getNetworkDetailOrderList(networkId, pageUtil));
    }

    @ApiOperation("获取服务商详情之工单费用列表")
    @PostMapping("/getNetworkDetailOrderFeeList/{networkId}")
    public Result getNetworkDetailOrderFeeList(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取服务商详情工单费用列表成功").data(conNetworkService.getNetworkDetailOrderFeeList(networkId, pageUtil));
    }

    @ApiOperation("获取服务商详情之工单配件列表")
    @PostMapping("/getNetworkDetailOrderSpareList/{networkId}")
    public Result getNetworkDetailOrderSpareList(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取服务商详情工单配件列表成功").data(conNetworkService.getNetworkDetailOrderSpareList(networkId, pageUtil));
    }

    @ApiOperation("获取服务商详情之服务商结算")
    @PostMapping("/getNetworkDetailModuleSettlementList/{networkId}")
    public Result getNetworkDetailModuleSettlementList(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取服务商详情服务商结算列表成功").data(conNetworkService.getNetworkDetailModuleSettlementList(networkId, pageUtil));
    }

    @ApiOperation("获取服务商详情之资质认证信息")
    @GetMapping("/getNetworkDetailAuthenField/{networkId}")
    public Result getNetworkDetailAuthenField(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId) {
        return Result.success("获取服务商详情资质认证信息成功").data(PageUtil.rows(conNetworkService.getNetworkDetailAuthenField(networkId)));
    }

    @ApiOperation("获取修改服务商前的信息")
    @GetMapping("/getBeforeNetworkEditSelect/{networkId}")
    public Result getBeforeNetworkEditSelect(@PathVariable @ApiParam(value = "服务商ID", required = true) Long networkId) {
        return Result.success("获取修改服务商前的信息成功").data("row",networkService.getBeforeNetworkEditSelect(networkId));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, remark = "服务区域-服务商列表 - 批量启用或禁用服务商")
    @ApiOperation("批量启用或禁用服务商")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "服务商状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "服务商ID", required = true)List<Long> ids) {
        networkService.updateDisableBatchById(ids, status);
        return Result.success(status? "禁用服务商成功" : "启用服务商成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.DELETE, remark = "服务区域-服务商列表 - 批量删除服务商")
    @ApiOperation("批量删除服务商")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "服务商ID", required = true)List<Long> ids) {
        conNetworkService.deleteBatchById(ids);
        return Result.success("删除服务商成功");
    }

    @ApiOperation("根据服务商ID获取服务商个人或团队变更申请")
    @GetMapping("/getNetworkWaitCheckAloneTeamChangeApply/{id}")
    public Result getNetworkWaitCheckAloneTeamChangeApply(@PathVariable @ApiParam(value = "服务商ID", required = true) Long id) {
        return Result.success("获取服务商个人或团队变更申请记录成功").data(PageUtil.row(networkAloneTeamChangeService.getNetworkWaitCheckAloneTeamChangeApply(id)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, remark = "服务区域-服务商列表 - 审核服务商个人或团队变更申请")
    @ApiOperation("审核服务商个人或团队变更申请")
    @PostMapping("/checkNetworkWaitCheckAloneTeamChangeApply")
    public Result checkNetworkWaitCheckAloneTeamChangeApply(HttpServletRequest request, @RequestBody @Valid NetworkAloneTeamChangeCheckVo changeCheckVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        networkAloneTeamChangeService.checkNetworkWaitCheckAloneTeamChangeApply(loginInfo, changeCheckVo);
        return Result.success("审核成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, type = OperationLogType.UPDATE, remark = "服务区域-服务商列表 - 批量更改团队模式")
    @ApiOperation("批量更改团队模式")
    @PostMapping("/updateNetworkTeamType")
    public Result updateNetworkTeamType(HttpServletRequest request, @RequestParam @ApiParam(value = "团队类型：1-团队，2-个人", required = true, example = "1") Integer teamType,
                                        @RequestBody @ApiParam(value = "服务商ID", required = true) List<Long> ids) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        conNetworkService.updateNetworkTeamType(custAccId, ids, teamType);
        return Result.success("批量更改团队模式成功");
    }


}
